package com.example.monotarr;

import java.util.List;
import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;

public class MyArrayAdapter extends ArrayAdapter<Struct>  {
	private final Activity context;
	//private final ArrayList<Struct> data;
	private final List<Struct> list;
	private int marcat_ad;
	private String fet;
	private Main main;	

	public MyArrayAdapter(Activity context, List<Struct> list, String fet) {
		super(context, R.layout.item, list);
		this.context = context;
		this.list = list;
		this.fet = fet;
		this.main = (Main) context;		
	}
	
	static class ViewHolder {
		public View icon;
		public TextView titol;
		public TextView notes;
		public TextView dia;
		public TextView hora;
		public CheckBox cb;
		//public TextView guio;
		
	}
	
	private void marca(int position, boolean ch, ViewHolder holder) {
		
		ListSQLiteHelper usdbh = new ListSQLiteHelper(getContext(), "DBList", null, 1);
		SQLiteDatabase db2 = usdbh.getWritableDatabase();

		
		if (ch) {
			holder.titol.setPaintFlags(holder.titol.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
			holder.notes.setPaintFlags(holder.titol.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
			holder.dia.setPaintFlags(holder.titol.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
			holder.hora.setPaintFlags(holder.titol.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
			//holder.cb.setChecked(true);
			
		
			marcat_ad = 1;			
			Struct s = list.get(position);			
			s.marcat = marcat_ad;
			list.set(position, s);
			
			db2.execSQL("UPDATE List SET marcat = " + marcat_ad + " WHERE id = " + list.get(position).id);

			db2.close();
			
			if (fet.equals("nofetes")) {
				//list.remove(position); 
				//this.notifyDataSetChanged();
				Toast.makeText(main, "tasca feta!", Toast.LENGTH_SHORT).show();
				main.actualitzaBD();
			}
			//else this.notifyDataSetChanged();
		}
		else {
			holder.titol.setPaintFlags(holder.titol.getPaintFlags() & 257);
			holder.notes.setPaintFlags(holder.titol.getPaintFlags() & 257);
			holder.dia.setPaintFlags(holder.titol.getPaintFlags() & 257);
			holder.hora.setPaintFlags(holder.titol.getPaintFlags() & 257);
			//holder.cb.setChecked(false);
			
			marcat_ad = 0;			
			Struct s = list.get(position);
			s.marcat = marcat_ad;
			list.set(position, s);
			
			db2.execSQL("UPDATE List SET marcat = " + marcat_ad + " WHERE id = " + list.get(position).id);

			db2.close();
			
			if (fet.equals("fetes")) {
				//list.remove(position);
				//this.notifyDataSetChanged();
				Toast.makeText(main, "tasca desmarcada", Toast.LENGTH_SHORT).show();
				main.actualitzaBD();

			}
			//else this.notifyDataSetChanged();
		}
		
		

	}
	

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		View view = null;
		
		if (convertView == null) {
			LayoutInflater inflater = context.getLayoutInflater();
			view = inflater.inflate(R.layout.item, null, true);
			final ViewHolder viewHolder = new ViewHolder();
			viewHolder.icon = (View) view.findViewById(R.id.icon);
			viewHolder.titol = (TextView) view.findViewById(R.id.titol);
			viewHolder.notes = (TextView) view.findViewById(R.id.notes);
			viewHolder.dia = (TextView) view.findViewById(R.id.dia);
			viewHolder.hora = (TextView) view.findViewById(R.id.hora);
			//viewHolder.guio = (TextView) view.findViewById(R.id.guio);
			viewHolder.cb = (CheckBox) view.findViewById(R.id.cbl);

			
			
			view.setTag(viewHolder);
			viewHolder.cb.setTag(list.get(position));
		} 
		else {
			view = convertView;
			((ViewHolder) view.getTag()).cb.setTag(list.get(position));
		}
		
		
		/*
		if (position % 2 == 0) {
			view.setBackgroundColor(Color.argb(0x77, 0xb8, 0xb8, 0xb8));
		}
		else {
			view.setBackgroundColor(Color.parseColor("#600000E0"));
		}
        */
            

		
		
		final ViewHolder holder = (ViewHolder) view.getTag();
		
		

		
		
		holder.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

		     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		            	marca(position, isChecked, holder);
		            
		     }
		    });
		
		//titol
		holder.titol.setText(list.get(position).titol);
		

		//notes
		holder.notes.setText(list.get(position).notes);
		if (list.get(position).notes.length() == 0) {
			holder.notes.setVisibility(View.GONE);
		}
		else holder.notes.setVisibility(View.VISIBLE);
		
		//prioritat
		if (list.get(position).prioritat == 1) {
			
			holder.icon.setBackgroundColor(Color.rgb(205, 0, 0));
			//holder.icon.setBackgroundColor(Color.parseColor("#DD1812"));
		}
		else if (list.get(position).prioritat == 2) {
	
			holder.icon.setBackgroundColor(Color.rgb(255, 140, 0));
			//holder.icon.setBackgroundColor(Color.parseColor("#FCCA03"));
		}
		else {
			
			holder.icon.setBackgroundColor(Color.rgb(35,142,35));
			//holder.icon.setBackgroundColor(Color.parseColor("#16A61E"));
		}
		
		//list
		if (list.get(position).dia != 99) {
			holder.dia.setVisibility(View.VISIBLE);
			holder.dia.setText(list.get(position).dia + "/" + list.get(position).mes + "/" + list.get(position).any);
		
		}
		else {
			holder.dia.setVisibility(View.GONE);
		}
		
		//hora
		if (list.get(position).hora != 99) {
			holder.hora.setVisibility(View.VISIBLE);
			String aux = list.get(position).hora + ":";
			if (list.get(position).minut < 10) aux += "0";
			holder.hora.setText(aux + list.get(position).minut);
		}
		else {
			holder.hora.setVisibility(View.GONE);
		}
		
		//guio
//		if (list.get(position).dia != 99 || list.get(position).hora != 99) {
//			holder.guio.setVisibility(View.VISIBLE);
//		}
//		else {
//			holder.guio.setVisibility(View.GONE);
//		}
//		
		
		//ratllat
		if (list.get(position).marcat == 1) {
				holder.titol.setPaintFlags(holder.titol.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
				holder.notes.setPaintFlags(holder.titol.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
				holder.dia.setPaintFlags(holder.titol.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
				holder.hora.setPaintFlags(holder.titol.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
				holder.cb.setChecked(true);
				
		}
		else {
			holder.titol.setPaintFlags(holder.titol.getPaintFlags() & 257);
			holder.notes.setPaintFlags(holder.titol.getPaintFlags() & 257);
			holder.dia.setPaintFlags(holder.titol.getPaintFlags() & 257);
			holder.hora.setPaintFlags(holder.titol.getPaintFlags() & 257);
			holder.cb.setChecked(false);
		}
		
	
		//mida
		holder.titol.setTextSize(Main.mida);
		holder.notes.setTextSize(Main.mida);
		holder.dia.setTextSize(Main.mida);
		holder.hora.setTextSize(Main.mida);
		//holder.guio.setTextSize(Main.mida);
		
		
		return view;
	}

}



